package jude.java.money.seo;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.Date;

import jude.java.util.DateUtils;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;


public class RapidIndexer implements Runnable {
	public static final String TEMPLATE1 = "/res/RapidIndexer.txt";		//53749
	public static final String TEMPLATE2 = "/res/RapidIndexer1.txt";	//10483
	public static final String TEMPLATE3 = "/res/RapidIndexer2.txt";	//xx
	
//	private static final String INDEX_TEMPLATE = "E:\\docs\\RapidIndexer.txt";
	private static final String OUTPUT_PATH = "E:\\batch\\index\\";
	private static final String KEY = "{website}";
	
	private String template;
	private String domain;
	private boolean readContent;
	
	private boolean saveResult;
	private String name;
	private Writer successOut;
	private Writer failureOut;
	
	public RapidIndexer(String domain) {
		this.domain = domain;
		this.template = TEMPLATE2;
		this.saveResult = true;
		this.readContent = true;
	}

	@Override
	public void run() {
		try {
			prepare();
			process();
			finish();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void prepare() throws UnsupportedEncodingException, FileNotFoundException {
		if (saveResult && failureOut == null) {
			new File(OUTPUT_PATH).mkdirs();
			String name = this.name;
			if (StringUtils.isEmpty(name))
				name = DateUtils.toString(new Date(), "MM-dd HH-mm");
			successOut = new OutputStreamWriter(new FileOutputStream(OUTPUT_PATH + name + ".success.txt", true), "UTF-8");
			failureOut = new OutputStreamWriter(new FileOutputStream(OUTPUT_PATH + name + ".failure.txt", true), "UTF-8");
		}
	}
	
	private void process() throws IOException {
		String path = getClass().getPackage().getName().replace('.', '/') + template;
		InputStream in = getClass().getClassLoader().getResourceAsStream(path);
		BufferedReader reader = new BufferedReader(new InputStreamReader(in), 4096);
		String line;
		while((line = reader.readLine()) != null) {
			if (!line.contains(KEY))
				continue;
			String url = StringUtils.replace(line, KEY, this.domain);
			visit(url);
		}
	}
	
	private void finish() throws IOException {
		if (saveResult && failureOut != null) {
			successOut.close();
			failureOut.close();
		}
	}

	private void visit(String url) throws ClientProtocolException, IOException {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet("http://localhost/");
		HttpResponse response = client.execute(get);
		int c = response.getStatusLine().getStatusCode();
		if (readContent) {
			HttpEntity entity = response.getEntity();
			if (entity != null) {
			    InputStream instream = entity.getContent();
			    byte[] tmp = new byte[2048];
			    instream.read(tmp);
			}
		}
		client.getConnectionManager().shutdown();
		if (c == 200) {
			success(url);
		} else {
			failure(url);
		}
	}
	
	private void success(String url) throws IOException {
		System.out.println("S: " + url);
		if (saveResult) {
			successOut.write(url);
			successOut.write("\r\n");
		}
	}
	
	private void failure(String url) throws IOException {
		System.out.println("F: " + url);
		if (saveResult) {
			failureOut.write(url);
			failureOut.write("\r\n");
		}
	}
}
